#include "XSystem/XCore.h"

// Constuct the checkbox with the pre-defined data.
XCheckBox::XCheckBox(XResourcesManager *data, int positionX, int positionY) : XWidget(data, 20, 20, positionX, positionY)
{
	x_type = XG_TYP_CHECKBOX;

	x_checkboxSprite.SetImage(x_data->GetImage("gui/checkbox.png"));
	x_checkboxCheckedSprite.SetImage(x_data->GetImage("gui/checkbox.png"));

	x_checkboxSprite.SetPosition(x_positionX, x_positionY);
	x_checkboxCheckedSprite.SetPosition(x_positionX, x_positionY);

	x_checkboxSprite.SetSubRect(sf::IntRect(0, 0, 14, 14));
	x_checkboxCheckedSprite.SetSubRect(sf::IntRect(45, 0, 59, 14));

	x_isChecked = false;
}

// Draws the checkbox, depending of the state.
void XCheckBox::Draw()
{
	x_app->Draw(x_checkboxSprite);

	if (x_isChecked)
		x_app->Draw(x_checkboxCheckedSprite);
}

// Check the state of the checkbox.
void XCheckBox::CheckState(const sf::Input &input)
{
	XWidget::CheckState(input);

	switch (x_state)
	{
		case XG_ST_MOUSE_ENTER:
			x_checkboxSprite.SetSubRect(sf::IntRect(15, 0, 29, 14));
			break;

		case XG_ST_MOUSE_EXIT:
			x_checkboxSprite.SetSubRect(sf::IntRect(0, 0, 14, 14));
			break;

		case XG_ST_LBUTTON_DOWN:
			x_checkboxSprite.SetSubRect(sf::IntRect(30, 0, 44, 14));
			break;

		case XG_ST_LBUTTON_RELEASED:
			x_checkboxSprite.SetSubRect(sf::IntRect(15, 0, 29, 14));
			x_isChecked = (x_isChecked + 1) % 2;
			break;
	}
}

// Set the checkbox to check or uncheck.
void XCheckBox::SetCheck(bool isChecked)
{
	x_isChecked = isChecked;
}

// \return if the checkbox is checked or not.
bool XCheckBox::IsChecked()
{
	return x_isChecked;
}
